<?
	class Query {
		var $DBType;

		var $Table;
		var $Field;

		var $BaseWhereQuery;
		var $Metadata;
		var $SMetadata;

		var $WhereField;
		var $WhereValue;
		var $WhereAnd;
		var $WhereOp;
		var $WhereQueryString;

		var $QueryWhereField;
		var $QueryWhereValue;
		var $QueryWhereAnd;
		var $QueryWhereOp;

		var $OrderField;
		var $QueryOrderField;
		var $OrderQueryString;

		var $GroupField;
		var $GroupQueryString;
		var $HavingQueryString;

		var $Limit;
		var $LimitNumber;

		function Query() {
			$this->DBType = 'oracle';
		}

		function setTable($getTable) {
			$this->Table = $getTable;
		}

		function nullAll() {
			$this->nullField();
			$this->nullWhere();
			$this->nullOrder();
			$this->nullLimit();
			$this->nullGroup();
			$this->BaseWhereQuery = '';
		}

		function nullField() {
			unset($this->Field);
		}

		function nullLimit() {
			unset($this->Limit);
			unset($this->LimitNumber);
		}

		function nullGroup() {
			unset($this->GroupField);
		}

		function setValue($getField, $getValue) {
			$index = array_keys($this->Field);
			$key = array_search($getField);
			$this->Field[$key] = $getValue;
		}

		function setLimit($getStart, $getNumber='') {
			$this->nullLimit();
			if($getStart>=0) {
				$this->Limit = $getStart;
				$this->LimitNumber = $getNumber;
			}
		}

		function setGroup($getGroup) {
			unset($this->GroupField);
			$this->GroupField[] = $getGroup;
		}

		function addGroup($getGroup) {
			$this->GroupField[] = $getGroup;
		}

		function setHavingQuery($getHaving) {
			$this->HavingQueryString = $getHaving;
		}

		function setField($getField, $getValue='', $getQuote='') {
			$this->nullField();
			$this->baseAddField($getField, $getValue, $getQuote);
		}

		function addField($getField, $getValue='', $getQuote='') {
			$this->baseAddField($getField, $getValue, $getQuote);
		}

		function setFieldArray($ary) {
			$this->nullField();
			$this->addFieldArray($ary);
		}

		function addFieldArray($ary) {
			if($ary)foreach($ary as $key => $value) {
				$this->baseAddField($key, $value);
			}
		}

		function baseAddField($getField, $getValue='', $getQuote=0) {
			if($getField!='') {
				if($getQuote!=1) {
					$Qt = '\'';
				}
				$this->Field[$this->MagicCheck($getField)] = $Qt.$this->MagicCheck($getValue).$Qt;
			}
		}

		function nullWhere() {
			unset($this->WhereField);
			unset($this->WhereValue);
			unset($this->WhereAnd);
			unset($this->WhereOp);
		}

		function nullQueryWhere() {
			unset($this->QueryWhereField);
			unset($this->QueryWhereValue);
			unset($this->QueryWhereAnd);
			unset($this->QueryWhereOp);
		}

		function setWhereAQ($getField, $getValue, $getOp = '=', $getAndOr='and') {
			$this->nullWhere();
			$this->Metadata[$getField] = 1;
			$count = func_num_args();
			if($count>2) {
				$tmp = $getValue;
				$getValue = $getOp;
				$getOp = $tmp;
			}
			$this->baseAddWhere($getField	, $getValue, $getOp, $getAndOr);
		}

		function setWhere($getField, $getValue, $getOp = '=', $getAndOr='and') {
			unset($this->Metadata[$getField]);
			$this->nullWhere();
			$count = func_num_args();
			if($count>2) {
				$tmp = $getValue;
				$getValue = $getOp;
				$getOp = $tmp;
			}
			$this->baseAddWhere($getField, $getValue, $getOp, $getAndOr);
		}

		function addWhereAQ($getField, $getValue, $getOp = '=', $getAndOr='and') {
			$this->Metadata[$getField] = 1;
			$count = func_num_args();
			if($count>2) {
				$tmp = $getValue;
				$getValue = $getOp;
				$getOp = $tmp;
			}
			$this->baseAddWhere($getField, $getValue, $getOp, $getAndOr);
		}
		function addWhere($getField, $getValue, $getOp = '=', $getAndOr='and') {
			unset($this->Metadata[$getField]);
			$count = func_num_args();
			if($count>2) {
				$tmp = $getValue;
				$getValue = $getOp;
				$getOp = $tmp;
			}
			$this->baseAddWhere($getField, $getValue, $getOp, $getAndOr);
		}

		function setWhereArray($ary) {
			$this->nullWhere();
			$this->addWhereArray($ary);
		}

		function addWhereArray($ary) {
			if($ary)foreach($ary as $key => $value) {
				$this->baseAddWhere($key, $value);
			}
		}

		function baseAddWhere($getField, $getValue, $getOp = '=', $getAndOr = 'and') {
			if($getField!='' && $getValue!='') {
				if($getAndOr=='and') {
					$getAndOr = 'and';
				} else {
					$getAndOr = 'or';
				}

				if($getValue=='') {
					return;
				}

				$this->WhereField[] =  $this->MagicCheck($getField);
				$this->WhereValue[] =  $this->MagicCheck($getValue);
				$this->WhereOp[] =  $this->MagicCheck($getOp);
				$this->WhereAnd[] =  $this->MagicCheck($getAndOr);
			}
		}

		function setMetadata($ary) {
			$this->Metadata = $ary;
		}

		function setSearchMetadata($ary) {
			$this->SMetadata = $ary;
		}

		function setWhereQuery($qry) {
			$this->BaseWhereQuery = $qry;
		}


		function setQueryWhere($getField, $getValue, $getOp = '=', $getAndOr='and') {
			$this->nullWhere();
			$this->addQueryWhere($getField, $getValue, $getOp, $getAndOr);
		}

		function addQueryWhere($getField, $getValue, $getOp = '=', $getAndOr='and') {
			$count = func_num_args();
			if($count>2) {
				$tmp = $getValue;
				$getValue = $getOp;
				$getOp = $tmp;
			}
			$this->baseAddQueryWhere($getField, $getValue, $getOp, $getAndOr);
		}

		function setQueryWhereArray($ary) {
			$this->nullQueryWhere();
			$this->addWQueryhereArray($ary);
		}

		function addQueryWhereArray($ary) {
			if($ary)foreach($ary as $key => $value) {
				$this->baseAddQueryWhere($key, $value);
			}
		}

		function baseAddQueryWhere($getField, $getValue, $getOp = '=', $getAndOr = 'and') {
			if(!is_null($getField)) {
				if($getAndOr!='or') {
					$getAndOr = 'and';
				} else {
					$getAndOr = 'or';
				}
				if($getOp=='') {
					$getOp = '=';
				}

				if($getValue=='') {
					return;
				}

				$this->QueryWhereField[] =  $this->MagicCheck($getField);
				$this->QueryWhereValue[] =  $this->MagicCheck($getValue);
				$this->QueryWhereOp[] =  $this->MagicCheck($getOp);
				$this->QueryWhereAnd[] =  $this->MagicCheck($getAndOr);
			}
		}

		function nullOrder() {
			unset($this->OrderField);
		}

		function nullQueryOrder() {
			unset($this->QueryOrderField);
		}

		function setOrder($getName, $getValue='') {
			$this->nullOrder();
			$this->addOrder($getName, $getValue);
		}

		function addOrder($getName, $getValue='') {
			if($getValue == 'desc' || $getValue == 'd') {
				$getValue = 'd';
			} else {
				$getValue = 'a';
			}
			$this->OrderField[$this->MagicCheck($getName)] = $getValue;
		}

		function setQueryOrder($getName, $getValue='') {
			$this->nullQueryOrder();
			$this->addQueryOrder($getName, $getValue);
		}

		function addQueryOrder($getName, $getValue='') {
			if($getValue == 'desc' || $getValue == 'd') {
				$getValue = 'd';
			} else {
				$getValue = 'a';
			}
			$this->QueryOrderField[$this->MagicCheck($getName)] = $getValue;
		}

		function FieldQuery() {
			$FieldQuery = ' ';
			if(is_array($this->Field)) {
				if($this->Field)foreach($this->Field as $Key => $Value) {
					$FieldQuery .= $Key.' ,';
				}
				$FieldQuery = substr($FieldQuery,0,strlen($FieldQuery)-1);
			} else {
				$FieldQuery = ' * ';
			}
			return $FieldQuery;
		}

		function ValueQuery() {
			if(is_array($this->Field)) {
				return join(',', $this->Field);
			}
		}

		function GroupQuery() {
			if(is_array($this->GroupField)) {
				$GroupFieldString =  'Group By '.join(',', $this->GroupField).' '.$this->HavingQueryString.' ';
				return $GroupFieldString;
			}
		}

		function WhereQuery() {
			$WhereQuery = '';
			$count = count($this->WhereField);

			if($count>0) {
				if($this->WhereField) {
					if($this->WhereField)foreach($this->WhereField as $Key => $Value) {
						$WhereQuery .= ' '.$Value.' ';

						if($this->Metadata[$Value]>0) {
							$Quata = '\'';
						} else {
							$Quata = '';
						}

						if($this->Metadata[$Value]==2) {
							$Per = '%';
							$WhereQuery .= 'like ';
						} else {
							$Per = '';
							$WhereQuery .= $this->WhereOp[$Key].' ';
						}

						$Where2on = 1;
						$WhereQuery .= $Quata.$Per.$this->WhereValue[$Key].$Per.$Quata;
						$WhereQuery .= ' '.$this->WhereAnd[$Key];
					}
				}
			}
			$count = count($this->QueryWhereField);
			if($count>0) {
				if(!is_null($this->QueryWhereField)) {
					$this->WhereQueryString = '';
					if($this->QueryWhereField)foreach($this->QueryWhereField as $Key => $Value) {
						if(trim($Value)) {
							if($this->SMetadata)if(array_key_exists($Value, $this->SMetadata)) {
								$WhereQuery .= ' '.$Value.' ';

								if($this->SMetadata[$Value]>0) {
									$Quata = '\'';
								} else {
									$Quata = '';
								}

									
								if($this->SMetadata[$Value]==2) {
									$Per = '%';
									$WhereQuery .= 'like ';
								} else {
									$Per = '';
									$WhereQuery .= $this->QueryWhereOp[$Key].' ';
								}

								$Where3on = 1;
								$WhereQuery .= $Quata.$Per.$this->QueryWhereValue[$Key].$Per.$Quata;
								$WhereQuery .= ' '.$this->QueryWhereAnd[$Key];
								// 쿼리문자열 만들기
								$this->QueryString .= '&amp;'.$this->SVName.($Key+1).'='.$this->QueryWhereValue[$Key];
								$this->WhereQueryString .= '&amp;'.$Value.'='.urlencode($this->QueryWhereValue[$Key]);
							}
						}
					}
				}
			}

			if(trim($this->BaseWhereQuery)!='') {
				$Where1on = 1;
			}

			if($Where1on==1 || $Where2on==1 || $Where3on==1) {
				if($Where2on==1 || $Where3on==1) {
					$WhereQuery = substr($WhereQuery,0,strlen($WhereQuery)-3);
				}
				if($Where1on==1) {
					if($Where2on==1 || $Where3on==1) {
						$WhereQuery = '('.$WhereQuery.') and ';
					}
					$WhereQuery .= '('.$this->BaseWhereQuery.')';
				}
				$WhereQuery =' where '.$WhereQuery;
				$WhereOn = true;
			}

			if($this->DBType=='oracle' && $this->Limit!='') {
				if($WhereOn) {
					//LimitNumber
					$WhereQuery .= ' and (rownum <= '.$this->Limit.') ';
				} else {
					$WhereQuery = ' where (rownum <= '.$this->Limit.') ';
				}
			}

			return $WhereQuery;
		}

		function FromQuery() {
			$FromQuery = ' from '.$this->Table.' ';
			return $FromQuery;
		}

		function OrderQuery() {
			$OrderQuery = '';
			if($this->OrderField!='' || $this->QueryOrderField!='') {
				$OrderQuery = '';
				if(!array_key_exists($Key, $this->OrderField)) {
					if($this->QueryOrderField)foreach($this->QueryOrderField as $Key => $Value) {
						if(strtoupper($Value)=='A' || strtoupper($Value)=='ASC') {
							$tasc = 'asc';
						} else {
							$tasc = 'desc';
						}
						$OrderQuery .= $Key.' '.$tasc.',';
					}
				}

				$this->OrderQueryString = '';
				if($this->OrderField)foreach($this->OrderField as $Key => $Value) {
					if(strtoupper($Value)=='A' || strtoupper($Value)=='ASC') {
						$tasc = 'asc';
					} else {
						$tasc = 'desc';
					}
					$OrderQuery .= $Key.' '.$tasc.',';

					// 쿼리문자열 만들기
					$this->OrderQueryString .= '&amp;='.$Key.'='.$this->QueryWhereValue[$Key];
				}

				$OrderQuery = substr($OrderQuery,0,strlen($OrderQuery)-1);

				if($OrderQuery) {
					$OrderQuery = ' Order by '.$OrderQuery;
				}
			}
			return $OrderQuery;
		}

		function LimitQuery() {
			if($this->DBType=='mysql') {
				$LimitQuery = '';
				if(!is_null($this->Limit)) {
					$LimitQuery .= ' limit ';
					if($this->LimitNumber=='') {
						$LimitQuery .= $this->Limit;
					} else {
						$LimitQuery .= $this->Limit.', '.$this->LimitNumber;
					}
				}
				return $LimitQuery;
			} else if($this->DBType=='cubrid') {
				$LimitQuery = '';
				if(!is_null($this->Limit)) {
					if(!$this->OrderQuery()) {
						$LimitQuery.= ' Order by '.$this->IDKey;
					}
					$LimitQuery .= ' for orderby_num() between ';
					if($this->LimitNumber=='') {
						$LimitQuery .= '1 and '.$this->Limit;
					} else {
						$LimitQuery .= $this->Limit.' and '.$this->LimitNumber;
					}
				}
				return $LimitQuery;
			} elseif($this->DBType=='pgsql') {
				$LimitQuery = '';
				if(!is_null($this->Limit)) {
					$LimitQuery .= ' limit ';
					if($this->LimitNumber=='') {
						$LimitQuery = $this->Limit.' offset 0';
					} else {
						//if ($this->Limit==0) $this->Limit=$this->Limit;
						$LimitQuery .= $this->LimitNumber.' offset '.$this->Limit;
					}
				}
				return $LimitQuery;
			}
		}

		function TopQuery() {
			if($this->Limit > 0) {
				if($this->DBType=='mssql') {
					$TopQuery = ' top '.$this->Limit.' ';
				}
				return $TopQuery;
			}
		}

		function SelectQuery() {			 
			if($this->Table!='') {
				$this->Query = 'select ';
				$this->Query .= $this->TopQuery(); // if DBType is mssql or oracle..
				$this->Query .= $this->FieldQuery();
				$this->Query .= $this->FromQuery();
				$this->Query .= $this->WhereQuery();
				$this->Query .= $this->GroupQuery();
				$this->Query .= $this->OrderQuery();
				$this->Query .= $this->LimitQuery(); // if DBType is mysql ..
				return $this->Query;
			}
		}

		function InsertQuery() {
			if(!is_null($this->Table) && !is_null($this->Field)) {
				$this->Query = 'insert into '.$this->Table.' (';
				$this->Query .= $this->FieldQuery();
				$this->Query .= ') values (';
				$this->Query .= $this->ValueQuery();
				$this->Query .= ')';
				return $this->Query;
			}
		}

		function UpdateQuery() {
			if($this->Table!='' && $this->Field && ( ($this->WhereField ) || ($this->QueryWhereField )) ) {
				$this->Query = 'update '.$this->Table.' set ';
				if(is_array($this->Field))foreach($this->Field as $Key => $Value) {
					if($Value==null) {
						$av = ' is ';
					} else {
						$av = ' = ';
					}
					$this->Query .= $Key.$av.$Value.' ,';
				}
				$this->Query = substr($this->Query,0,strlen($this->Query)-1);
				$this->Query .= $this->WhereQuery();
				return $this->Query;
			}
		}

		function DeleteQuery() {
			if($this->Table!='' && ( ($this->WhereField ) || ($this->QueryWhereField )) ) {
				$this->Query = 'delete ';
				$this->Query .= $this->FromQuery();
				$this->Query .= $this->WhereQuery();
				return $this->Query;
			}
		}

		function MagicCheck($data)	{
			if($this->DBType=='mssql' || $this->DBType=='pgsql') {
				if(get_magic_quotes_gpc())	{
					$data = str_replace("\'", "'", $data);
					$data = str_replace('\"', '"', $data);
					$data = str_replace('\\', '', $data);
				}
				return str_replace("'", "''", trim($data));
			} else {
				if(!get_magic_quotes_gpc())	{
					return addslashes(trim($data));
				} else {
					return trim($data);
				}
			}
		}

		function addQ($str) {
			return '\''.$str.'\'';
		}

		function subQ($str) {
			if($this->DBType=='mysql') {
				return substr($str, 1, strlen($str)-1);
			} else if($this->DBType=='mssql') {
				return substr($str, 1, strlen($str)-1);
			} elseif($this->DBType=='oracle') {
				return substr($str, 2, strlen($str)-2);
			}
		}
	}
?>